import { Component } from '@angular/core';
import { Driver } from './driver';

import { OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { DriverService } from './driver.service';

@Component({
  selector: 'my-drivers',
  templateUrl: './driver.component.html'
})

export class DriverComponent implements OnInit {
  ngOnInit() {
    this.getDrivers();
  }

  drivers: Driver[];
  selectedDriver: Driver;

  onSelect(driver: Driver) { this.selectedDriver = driver; }

  onNew() { 
    this.selectedDriver = new Driver();
    this.selectedDriver.carrier_id = parseInt(localStorage.getItem('loginUsercarrierID'));
 }

  constructor(
    private router: Router,
    private driverService: DriverService) {
  }

  getDrivers() {
    if(localStorage.getItem('loginUsercarrierID') == '0' || localStorage.getItem('loginUsercarrierID') == null){
      this.driverService.getDrivers().then(drivers => this.drivers = drivers);
    }else{
      this.driverService.getDriversByCarrierID(localStorage.getItem('loginUsercarrierID')).then(drivers => this.drivers = drivers);
    }

  }
}
